package faceBook;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;

import twss.DataConnection;
import twss.DataObject;

/**
 * The Class Message.
 */
public class Message implements DataObject, JSONString
{
    
    /** The ID. */
    private long ID;
    
    /** The user. */
    private User user;
    
    /** The message. */
    private String message;
    
    /** The link. */
    private String link;
    
    /** The send date. */
    private Date sendDate;

    /**
     * Instantiates a new message.
     */
    public Message(){}

    /**
     * Instantiates a new message.
     * 
     * @param id
     *            the id
     */
    public Message(long id)
    {
        ID = id;
        load();
    }
    
    /**
     * Gets the user.
     * 
     * @return the user
     */
    public User getUser()
    {
        return user;
    }

    /**
     * Sets the user.
     * 
     * @param USER
     *            the new user
     */
    public void setUser(User USER)
    {
        this.user = USER;
    }

    /**
     * Gets the message.
     * 
     * @return the message
     */
    public String getMessage()
    {
        return message;
    }

    /**
     * Sets the message.
     * 
     * @param Message
     *            the new message
     */
    public void setMessage(String Message)
    {
        this.message = Message;
    }

    /**
     * Gets the link.
     * 
     * @return the link
     */
    public String getLink()
    {
        return link;
    }

    /**
     * Sets the link.
     * 
     * @param Link
     *            the new link
     */
    public void setLink(String Link)
    {
        this.link = Link;
    }

    /**
     * Gets the send date.
     * 
     * @return the send date
     */
    public Date getSendDate()
    {
        return sendDate;
    }

    /**
     * Sets the send date.
     * 
     * @param SendDate
     *            the new send date
     */
    public void setSendDate(Date SendDate)
    {
        this.sendDate = SendDate;
    }

    /* (non-Javadoc)
     * @see org.json.JSONString#toJSONString()
     */
    @Override
    public String toJSONString()
    {
        JSONObject JO = new JSONObject();
        try
        {
            JO.put("user", new JSONObject(user.toJSONString()));
            JO.put("message", message);
            JO.put("link", link);
            JO.put("sendDate", sendDate);
        } catch (JSONException e)
        {
            // Do Nothing
        }
        return JO.toString();
    }

    /* (non-Javadoc)
     * @see foxtrotbravo.DataObject#save()
     */
    @Override
    public boolean save()
    {
        try
        {
            if (ID != 0)
            {
                return update();
            }
            return insert();
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return false;
    }

    /* (non-Javadoc)
     * @see foxtrotbravo.DataObject#load()
     */
    @Override
    public boolean load()
    {
        try
        {
            if (DataConnection.connect())
            {
                String statement = "select user, message, link, sendDate from message where id = ?";
                PreparedStatement stmt = DataConnection.getConnection()
                        .prepareStatement(statement);
                stmt.setLong(1, ID);
                ResultSet results = stmt.executeQuery();
                if (results.next())
                {
                    user = new User(results.getLong("user"));
                    message = results.getString("message");
                    link = results.getString("link");
                    sendDate = results.getDate("sendDate");
                    return true;
                }
            }
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return false;
    }

    /* (non-Javadoc)
     * @see foxtrotbravo.DataObject#delete()
     */
    @Override
    public boolean delete()
    {
        try
        {
            if (DataConnection.connect())
            {
                String statement = "delete from message where id = ?";
                PreparedStatement stmt = DataConnection.getConnection()
                        .prepareStatement(statement);
                stmt.setLong(1, ID);
                int success = 2;
                success = stmt.executeUpdate();
                return success == 1;
            }
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * Update.
     * 
     * @return true, if successful
     * @throws SQLException
     *             the SQL exception
     */
    private boolean update() throws SQLException
    {
        if (DataConnection.connect())
        {
            String statement = "update message set user = ?, message = ?, link = ?, sendDate = ? where id = ?";
            PreparedStatement stmt = DataConnection.getConnection()
                    .prepareStatement(statement);
            stmt.setLong(1, user.getID());
            stmt.setString(2, message);
            stmt.setString(3, link);
            stmt.setDate(4, sendDate);
            stmt.setLong(5, ID);
            int success = 2;
            stmt.executeUpdate();
            return success == 1;
        }
        return false;
    }

    /**
     * Insert.
     * 
     * @return true, if successful
     * @throws SQLException
     *             the sQL exception
     */
    private boolean insert() throws SQLException
    {
        if (DataConnection.connect())
        {
            String statement = "INSERT INTO message (user, message, link, sendDate) VALUES ( ?, ?, ?, ? )";
            PreparedStatement stmt = DataConnection.getConnection()
                    .prepareStatement(statement,
                            Statement.RETURN_GENERATED_KEYS);
            stmt.setLong(1, user.getID());
            stmt.setString(2, message);
            stmt.setString(3, link);
            stmt.setDate(4, sendDate);
            int success = 2;
            stmt.executeUpdate();
            ResultSet key = stmt.getGeneratedKeys();
            if (key.next())
            {
                ID = key.getLong(1);
            }
            return success == 1;
        }
        return false;
    }

}
